package com.jlkf.temp;

/**
 * @author he.hb
 */
public class CommonUtils {

    /*去掉字符串首尾逗号*/
    public static String removeStartAndEndNode(String str) {
        if (str != null && str.length() > 0) {
            if (",".equals("" + str.charAt(0))) {
                str = str.substring(1, str.length());
            }
            if (",".equals("" + str.charAt(str.length() - 1))) {
                str = str.substring(0, str.length() - 1);
            }
        }
        return str;
    }

    /*将逗号隔开的 String 改造成 mybatis可用的 String */
    public static String commaSeparatedStr(String str) {
        if (str != null && !"".equals(str)) {
            str = removeStartAndEndNode(str);
            str = "\""+str.replace(",","\",\"")+"\"";
        }
        return str;
    }

    /* 计算个点之间的距离(经纬度) */
    public static double twoPointDistance(double latitude1,double longitude1,double latitude2,double longitude2){
        double lat1 = (Math.PI/180)*latitude1;
        double lat2 = (Math.PI/180)*latitude2;
        double lon1 = (Math.PI/180)*longitude2;
        double lon2 = (Math.PI/180)*longitude2;
        //地球半径
        double R = 6371;
        //两点间距离 km，如果想要米的话，结果*1000就可以了
        double d =  Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;
        return d*1000;
    }
}
